<!-- @author zhengjie -->
<template>
  <div class="icon-body">
    <div style="width: 100%;">
      <span style="width: 20%;">图标类型：</span>
      <el-select style="width: 75%;"
          v-model="iconType"
          @change="changeIconType"
          placeholder="请选择图标类型"
          clearable>
          <el-option
            v-for="item in iconTypeData"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <i class="el-icon-warning" v-show="iconType==20" title="如需增加自定义图标,将svg文件放入src/assets/icons/svg/customize目录下" style="margin-left: 5px;"></i>
    </div>
    <div style="width: 100%;margin-top: 5px;">
      <span style="width: 20%;">搜索图标：</span>
      <el-input v-model="name" class="icon-search" style="width: 75%;" clearable placeholder="请输入图标编号" @clear="filterIcons" @input="filterIcons">
        <i slot="suffix" class="el-icon-search el-input__icon" />
      </el-input>
    </div>
    <div class="icon-list">
      <div class="list-container">
        <div v-for="(item, index) in iconList" class="icon-item-wrapper" :key="index" @click="selectedIcon(item)">
          <div :class="['icon-item', { active: activeIcon === item }]">
            <svg-icon :icon-class="item" class-name="icon" style="height: 20px;width: 20px;"/>
            <span>{{ item }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import icons from './requireIcons'
export default {
  name: 'IconSelect',
  props: {
    activeIcon: {
      type: String
    }
  },
  data() {
    return {
      name: '',
      iconList: icons[0],
      iconType:0,
      iconTypeData:[
        {label:"若依",value:0,folderName:"ruoyi"},
        {label:"商业",value:2,folderName:"business"},
        {label:"通信",value:5,folderName:"communication"},
        {label:"设计与管理",value:7,folderName:"design-and-development"},
        {label:"物流",value:13,folderName:"LogisticsDelivery"},
        {label:"多媒体",value:16,folderName:"multimedia"},
        {label:"安全",value:19,folderName:"security"},
        {label:"自定义",value:20,folderName:"customize"},
      ]
    }
  },
  methods: {
    changeIconType(val){
      this.iconList = icons[this.iconType]
    },
    filterIcons() {
      this.iconList = icons[this.iconType]
      if (this.name) {
        this.iconList = this.iconList.filter(item => item.includes(this.name))
      }
    },
    selectedIcon(name) {
      this.$emit('selected', name)
      document.body.click()
    },
    reset() {
      this.name = ''
      // this.iconType = 0
      // this.iconList = icons[0]
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .icon-body {
    width: 100%;
    padding: 10px;
    .icon-search {
      position: relative;
      margin-bottom: 5px;
    }
    .icon-list {
      height: 200px;
      overflow: auto;
      .list-container {
        display: flex;
        flex-wrap: wrap;
        .icon-item-wrapper {
          width: calc(100% / 3);
          height: 25px;
          line-height: 25px;
          cursor: pointer;
          display: flex;
          .icon-item {
            display: flex;
            max-width: 100%;
            height: 100%;
            padding: 0 5px;
            &:hover {
              background: #ececec;
              border-radius: 5px;
            }
            .icon {
              flex-shrink: 0;
            }
            span {
              display: inline-block;
              vertical-align: -0.15em;
              fill: currentColor;
              padding-left: 2px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }
          .icon-item.active {
            background: #ececec;
            border-radius: 5px;
          }
        }
      }
    }
  }
</style>
